package com.cgy.utils.spider.handler;

import java.util.List;

import com.cgy.utils.spider.callback.AbstractCallBack;
import com.cgy.utils.spider.element.AbstractElement;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

/**
 * webmagic处理类
 *
 * @author CH
 */
public class WebMagicProcessor extends AbstractProcessor implements PageProcessor {

    protected Site site;
    protected AbstractCallBack callBack;

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
        List<AbstractElement> elements2 = getElements();
        for (AbstractElement element : elements2) {
            super.getResolver().setElement(element);
            super.getResolver().analysis(page, callBack);
        }
    }

    /**
     * 初始化
     *
     * @return
     */
    public static WebMagicProcessor create() {
        return new WebMagicProcessor();
    }

    /**
     * 配置site
     *
     * @param site
     * @return
     */
    public AbstractProcessor setSite(Site site) {
        this.site = site;
        return this;
    }

    /**
     *
     */
    public AbstractProcessor setCallBack(AbstractCallBack callBack) {
        this.callBack = callBack;
        return this;
    }

}
